package com.frank.millen;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class DataBase {

	static String url = "localhost";
	static String dbname = "millen";
	static String username = "root";
	static String password = "schock";
	static String driver = "com.mysql.jdbc.Driver";
	static DataBase INSTANCE = new DataBase();

	public static DataBase getInstance() {
		return INSTANCE;
	}

	public User checkLogin(String username, String password) {
		User user=null;
		try {
			Class.forName(driver).newInstance();
			Connection conn = DriverManager.getConnection(url + dbname,
					username, password);
			System.out.println("Connected to the database");
			
			PreparedStatement ps = conn.prepareStatement(
			  "SELECT username, password FROM users WHERE username = ? AND password= ?");
			ps.setString( 1, username );
			ps.setString( 2, password );
			ResultSet rs=ps.executeQuery();
			
			if (rs.first()) {				
				return user;
			}
			
			conn.close();
			System.out.println("Disconnected from database");		
			
		} catch (Exception e) {
			System.out.println("Problems with authentication");
			e.printStackTrace();
		}
		return user;
	}
}
